package wb.is.besti.edu.cd.javalibgame;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private int score = 0;
    private TextView tvScore, tvBestScore;
    private RelativeLayout root = null;
    //private Button btnNewGame;

    private GameView gameView;
    private AnimaLayer animLayer = null;
    public static final String SP_KEY_BEST_SCORE = "bestScore";
    private static MainActivity mainActivity = null;
    public MainActivity() {
        mainActivity = this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ActionBar actionBar = getSupportActionBar();
        actionBar.hide();
        root = (RelativeLayout) findViewById(R.id.container);
        root.setBackgroundResource(R.drawable.background02);
        UnfoldButton unfoldButton = (UnfoldButton)findViewById(R.id.unfoldButton);
        unfoldButton.addElement(R.drawable.regame, R.color.colorPrimary, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gameView.startGame();
            }
        });
        unfoldButton.addElement(R.drawable.ic_action_sun, R.color.darkcyan, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Config.setLINES(4);
                Intent intent = new Intent(MainActivity.this,MainActivity.class);
                startActivity(intent);
                finish();


            }
        });
        unfoldButton.addElement(R.drawable.ic_action_twitter, R.color.lightblue, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Config.setLINES(6);
                Intent intent = new Intent(MainActivity.this,MainActivity.class);
                startActivity(intent);
                finish();

            }
        });
        unfoldButton.setmRotatable(true);
        unfoldButton.setmScale(1);
        unfoldButton.setLength(250);
        unfoldButton.setAngle(90);


        tvScore = (TextView) findViewById(R.id.tvScore);
        tvScore.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/type.ttf"));
        tvScore.setTextColor(0xffF08080);
        tvBestScore = (TextView) findViewById(R.id.tvBestScore);
        tvBestScore.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/type.ttf"));
        tvBestScore.setTextColor(0xff800000);
        gameView = (GameView) findViewById(R.id.gameView);

        animLayer = (AnimaLayer) findViewById(R.id.animLayer);

    }
    public static MainActivity getMainActivity() {
        return mainActivity;
    }

    public AnimaLayer getAnimLayer() {
        return animLayer;
    }

    public void showBestScore(int s) {   //最高纪录的那个地方会显示出保存的最好的成绩
        tvBestScore.setText(s + "");
    }

    public int getBestScore() {
        return getPreferences(MODE_PRIVATE).getInt(SP_KEY_BEST_SCORE, 0);
    }

    public void saveBestScore(int s) {   //保存数据，将最大的值存到sd卡中
        SharedPreferences.Editor e = getPreferences(MODE_PRIVATE).edit();
        e.putInt(SP_KEY_BEST_SCORE, s);
        e.commit();
    }

    public void addScore(int s) {
        score += s;
        showScore();

        int maxScore = Math.max(score, getBestScore());
        saveBestScore(maxScore);
        showBestScore(maxScore);
    }

    public void showScore() {
        tvScore.setText(score + "");
    }

    public void clearScore() {
        score = 0;
        showScore();
    }
/*
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    */
}
